import { _decorator, Component, Node } from "cc";
import { speed } from "./Contanst";
const { ccclass, property } = _decorator;

@ccclass("MoveBg")
export class MoveBg extends Component {
  @property(Node)
  public target1: Node = null;

  @property(Node)
  public target2: Node = null;

  @property
  public moveSpeed = speed;

  private _canControl = true;

  protected start() {}

  protected update(deltaTime: number) {
    this.moveEle(deltaTime, this.target1, this.target2);
  }

  public moveEle(deltaTime: number, node1: Node, node2: Node): void {
    if (!this._canControl) return;
    node1.setPosition(
      node1.position.x - this.moveSpeed * deltaTime,
      node1.position.y
    );

    node2.setPosition(
      node2.position.x - this.moveSpeed * deltaTime,
      node2.position.y
    );

    if (node1.position.x <= -720) {
      node1.setPosition(node2.position.x + 720, node1.position.y);
    }

    if (node2.position.x <= -720) {
      node2.setPosition(node1.position.x + 720, node2.position.y);
    }
  }

  public enableMoving() {
    this._canControl = true;
  }

  public disableMoving() {
    this._canControl = false;
  }
}
